package hua.mtreader.feature.bookshelf.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import hua.mtreader.R;
import hua.mtreader.feature.bookshelf.bean.BookshelfRepsone;

/**
 * Created by Administrator on 2017/3/22.
 */
public class GridBookAdapter extends RecyclerView.Adapter<GridBookAdapter.ViewHolder> {


    private ImageLoader imageLoader;
    private List<BookshelfRepsone.DataBean.BookListBean> bookList;

    public GridBookAdapter() {
        imageLoader = ImageLoader.getInstance();
        bookList = new ArrayList<>();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View inflate = View.inflate(parent.getContext(), R.layout.item_bookshelf_grid, null);
        return new ViewHolder(inflate);
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        BookshelfRepsone.DataBean.BookListBean bean = bookList.get(position);
        holder.tvName.setText(bean.getName());
        imageLoader.displayImage(bean.getIcon(),holder.ivBook);

    }

    @Override
    public int getItemCount() {
        return bookList.size();
    }

    public void setData(List<BookshelfRepsone.DataBean.BookListBean> bookList) {
        if (bookList == null) {
            bookList = new ArrayList<>();
        }
        this.bookList.clear();
        this.bookList.addAll(bookList);
        notifyDataSetChanged();
    }

    static class ViewHolder extends RecyclerView.ViewHolder{

        private ImageView ivBook;
        private TextView tvName;
        private TextView tvRecord;

        public ViewHolder(View itemView) {
            super(itemView);
            ivBook = (ImageView) itemView.findViewById(R.id.iv_book);
            tvName = (TextView) itemView.findViewById(R.id.tv_name);
            tvRecord = (TextView) itemView.findViewById(R.id.tv_record);
        }
    }




}
